#include "lvgl_app/lvgl_app.h"
#include <stdio.h>
#include <stdlib.h>
/*枚举
界面ID
    PM_ID_SETTING_DATA

界面参数
    无
*/
LV_FONT_DECLARE(lv_siyuan_reg_8);/* 声明字体 */
#define OPTION_FMT       "%02d\n%02d\n%02d\n%02d\n%02d"    // 选项列表格式
#define MASTER_INDEX     2                                 // 列表为5项，中间项索引为2
#define OPTION_LEN       32                                // 列表字符串buffer长度
/**
 * @brief 选项列表最大值最小值
*/
typedef struct {
    int min;
    int max;
} roller_range_t;

static lv_obj_t* roller1, * roller2, * roller3, * roller4, * roller5, * roller6;
/**
 * @brief 计算选项列表
 * @param val 当前选中的值
 * @param buf 选项列表字符串buffer
 * @param len 选项列表字符串buffer长度
 * @param min 控件选项列表最小值
 * @param max 控件选项列表最大值
*/
static void calc_roller_option(int val, char* buf, int len, int min, int max)
{
    int left1 = val - 1;
    int left2 = val - 2;
    int right1 = val + 1;
    int right2 = val + 2;
    int range = max - min + 1;

    if (left1 < min) {
        left1 += range;
    }

    if (left2 < min) {
        left2 += range;
    }

    if (right1 > max) {
        right1 -= range;
    }

    if (right2 > max) {
        right2 -= range;
    }

    snprintf(buf, len, OPTION_FMT, left2, left1, val, right1, right2);

    return;
}

/**
 * @brief roller控件事件回调函数
 * @param e
*/
static void roller_event_cb(lv_event_t* e)
{
    if (LV_EVENT_KEY == lv_event_get_code(e)) {
        lv_obj_t* obj = lv_event_get_target(e);

        roller_range_t* range = (roller_range_t*)(obj->user_data);

        char buf[OPTION_LEN] = "\0";
        int val = 0;
        lv_roller_get_selected_str(obj, buf, OPTION_LEN);
        val = atoi(buf);

        calc_roller_option(val, buf, OPTION_LEN, range->min, range->max);
        lv_roller_set_options(obj, buf, LV_ROLLER_MODE_NORMAL);    // 这里不需要设置无限模式，计算列表时已处理列表循环
        lv_roller_set_selected(obj, MASTER_INDEX, LV_ANIM_OFF);
    }

    return;
}

/**
 * @brief 创建roller控件
 * @param parent 父窗体
 * @param def_val 默认值
 * @param min_val 最小值
 * @param max_val 最大值
 * @return 已创建的roller对象指针
*/
static lv_obj_t* create_roller(lv_obj_t* parent, int def_val, int min_val, int max_val)
{
    lv_obj_t* roller = lv_roller_create(parent);
    roller_range_t* range = (roller_range_t*)lv_mem_alloc(sizeof(roller_range_t));

    /* 先对一些数据进行隐藏 */
    lv_obj_set_style_bg_opa(roller, 0, 0);
    lv_obj_set_style_border_opa(roller, 0, 0);
    lv_obj_set_style_bg_opa(roller, 0, LV_PART_SELECTED);
    lv_obj_set_style_outline_opa(roller, 0, LV_STATE_FOCUS_KEY);
    /* 再去设置突出先的颜色 */
    lv_obj_set_style_text_color(roller, lv_color_make(33, 150, 243), LV_PART_SELECTED);
    lv_obj_set_style_text_color(roller, lv_color_make(254, 91, 76), LV_PART_SELECTED|LV_STATE_FOCUS_KEY);


    if (NULL != range) {
        range->min = min_val;
        range->max = max_val;
    }

    roller->user_data = (void*)range;

    char option[OPTION_LEN] = "\0";
    calc_roller_option(def_val, option, OPTION_LEN, min_val, max_val);

    lv_roller_set_options(roller, option, LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(roller, MASTER_INDEX, LV_ANIM_OFF);
    lv_obj_add_event_cb(roller, roller_event_cb, LV_EVENT_KEY, NULL);/* 每当触发按键就重新计算一次新的数据 */

    return roller;
}

/**
 * @brief 设置roller当前值
 * @param roller 控件对象
 * @param val 当前值
*/
static void set_roller_cur_val(lv_obj_t* roller, int val)
{
    roller_range_t* range = (roller_range_t*)(roller->user_data);
    char buf[OPTION_LEN] = "\0";

    calc_roller_option(val, buf, OPTION_LEN, range->min, range->max);
    lv_roller_set_options(roller, buf, LV_ROLLER_MODE_NORMAL);
    lv_roller_set_selected(roller, MASTER_INDEX, LV_ANIM_OFF);
}

/**
 * @brief tileview控件事件回调函数
 * @param e
*/
static void tileview_event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);

    if (code == LV_EVENT_FOCUSED)
    {
        lv_obj_t* obj = lv_event_get_current_target(e);
        if (obj == roller1 || obj == roller3)
        {
            lv_obj_set_tile_id(e->user_data, 0, 0, LV_ANIM_ON);
        }
        else if (obj == roller4 || obj == roller6)
        {
            lv_obj_set_tile_id(e->user_data, 1, 0, LV_ANIM_ON);
        }
    }
    if (code == LV_EVENT_SHORT_CLICKED)
    {
 
    }

    return;
}

/**
 * @brief btn控件事件回调函数
 * @param e
*/
static void btn_event_handler(lv_event_t* event)
{
    lv_event_code_t code = lv_event_get_code(event);
    if (code == LV_EVENT_FOCUSED)
    {
        lv_obj_set_tile_id(event->user_data, 2, 0, LV_ANIM_ON);
    }
    else if (code == LV_EVENT_SHORT_CLICKED)
    {
        char str_temp[15];
        Myos_Time time_set;
        lv_pm_printf("开始获取设置时间\r\n");
        lv_roller_get_selected_str(roller1, str_temp, sizeof(str_temp));
        time_set.year = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        lv_roller_get_selected_str(roller2, str_temp, sizeof(str_temp));
        time_set.mon = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        lv_roller_get_selected_str(roller3, str_temp, sizeof(str_temp));
        time_set.day = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        lv_roller_get_selected_str(roller4, str_temp, sizeof(str_temp));
        time_set.hour = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        lv_roller_get_selected_str(roller5, str_temp, sizeof(str_temp));
        time_set.min = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        lv_roller_get_selected_str(roller6, str_temp, sizeof(str_temp));
        time_set.sec = atoi(str_temp);
        lv_pm_printf("%s - ", str_temp);
        
        OS_SystemTimeSet(&time_set);
        
        
        lv_pm_control.back();
    }
}

static void page_onLoad(void* pm_page)
{
    lv_obj_t* page = lv_pm_get_pm_page(pm_page)->page;

    lv_obj_t* tv = lv_tileview_create(page);
    lv_obj_set_scrollbar_mode(tv, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_style_bg_opa(tv, 0, 0);

    lv_obj_t* tile1 = lv_tileview_add_tile(tv, 0, 0, LV_DIR_RIGHT);
    lv_obj_t* tile2 = lv_tileview_add_tile(tv, 1, 0, LV_DIR_RIGHT | LV_DIR_LEFT);
    lv_obj_t* tile3 = lv_tileview_add_tile(tv, 2, 0, LV_DIR_LEFT);

    extern Myos_MessageInterface  *User_MessageInterface;                       //系统数据信息接口
    
    roller1 = create_roller(tile1, User_MessageInterface->Myos_time->year, 2000, 9999);
    lv_roller_set_visible_row_count(roller1, 3);
    lv_obj_align(roller1, LV_ALIGN_CENTER, -70, 0);
    lv_obj_add_event_cb(roller1, tileview_event_cb, LV_EVENT_ALL, tv);/* 每当触发按键就重新计算一次新的数据 */
    lv_obj_t* btn_lab = lv_label_create(tile1);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller1, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "年");

    roller2 = create_roller(tile1, User_MessageInterface->Myos_time->mon, 0, 12);
    lv_roller_set_visible_row_count(roller2, 3);
    lv_obj_align(roller2, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(roller2, tileview_event_cb, LV_EVENT_SHORT_CLICKED, tv);
    btn_lab = lv_label_create(tile1);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller2, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "月");

    roller3 = create_roller(tile1, User_MessageInterface->Myos_time->day, 0, 31);
    lv_roller_set_visible_row_count(roller3, 3);
    lv_obj_align(roller3, LV_ALIGN_CENTER, 70, 0);
    lv_obj_add_event_cb(roller3, tileview_event_cb, LV_EVENT_ALL, tv);
    btn_lab = lv_label_create(tile1);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller3, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "日");

    roller4 = create_roller(tile2, User_MessageInterface->Myos_time->hour, 0, 23);
    lv_roller_set_visible_row_count(roller4, 3);
    lv_obj_align(roller4, LV_ALIGN_CENTER, -70, 0);
    lv_obj_add_event_cb(roller4, tileview_event_cb, LV_EVENT_ALL, tv);/* 每当触发按键就重新计算一次新的数据 */
    btn_lab = lv_label_create(tile2);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller4, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "时");

    roller5 = create_roller(tile2, User_MessageInterface->Myos_time->min, 0, 59);
    lv_roller_set_visible_row_count(roller5, 3);
    lv_obj_align(roller5, LV_ALIGN_CENTER, 0, 0);
    lv_obj_add_event_cb(roller5, tileview_event_cb, LV_EVENT_SHORT_CLICKED, tv);
    btn_lab = lv_label_create(tile2);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller5, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "分");

    roller6 = create_roller(tile2, User_MessageInterface->Myos_time->sec, 0, 59);
    lv_roller_set_visible_row_count(roller6, 3);
    lv_obj_align(roller6, LV_ALIGN_CENTER, 70, 0);
    lv_obj_add_event_cb(roller6, tileview_event_cb, LV_EVENT_ALL, tv);
    btn_lab = lv_label_create(tile2);
    lv_obj_set_style_text_font(btn_lab, &lv_siyuan_reg_8, LV_STATE_DEFAULT);
    lv_obj_align_to(btn_lab, roller6, LV_ALIGN_OUT_RIGHT_MID, -10, -5);
    lv_label_set_text(btn_lab, "秒");

    lv_obj_t* btn_class = lv_btn_create(tile3);
    lv_obj_set_style_bg_color(btn_class, lv_palette_main(LV_PALETTE_GREY), LV_PART_MAIN);

    lv_obj_set_size(btn_class, 30, 30);
    lv_obj_add_event_cb(btn_class, btn_event_handler, LV_EVENT_ALL, tv);
    lv_obj_align(btn_class, LV_ALIGN_CENTER, 0, 0);

    btn_lab = lv_label_create(btn_class);
    lv_label_set_text(btn_lab, "app");
    lv_obj_align(btn_lab, LV_ALIGN_CENTER, 0, 0);

    lv_pm_create.focus_obj(pm_page, roller1);



}

static void page_unLoad(void* pm_page)
{
    lv_mem_free(roller1->user_data);
    lv_mem_free(roller2->user_data);
    lv_mem_free(roller3->user_data);
}

void page_create_page_setting_time(lv_pm_id_t id)
{
    lv_pm_page_t* page = lv_pm_create.create(id, page_onLoad, page_unLoad);
    lv_pm_create.status_bar(page, false);
}
